package com.cloud.controller.rule;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bean.rule.Rule;
import com.cloud.bean.user.User;
import com.cloud.common.BaseReturn;
import com.cloud.service.rule.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.cloud.common.JwtUtils.getByToken;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyuan
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/rule")
public class RuleController {
    @Autowired
    RuleService ruleService;

    @RequestMapping("/listAll")
    public BaseReturn listAll(){
        List<Rule> list = ruleService.list();
        return BaseReturn.build("2000","查询成功",list);
    }


    @RequestMapping("/queryList")
    public BaseReturn queryList(@RequestBody FromRule fromRule){
        User user = getByToken();
        if(user == null){
            return BaseReturn.build("4003","登录失效");
        }
        fromRule.setCustomerOn(user.getCustomerNo());
        Page<Rule> fromRulePage = new Page<>(fromRule.getPage(),fromRule.getSize());
        QueryWrapper<Rule> queryWrapper = new QueryWrapper<>(fromRule);
        IPage<Rule> page = ruleService.page(fromRulePage, queryWrapper);
        return  BaseReturn.build("2000","查询成功",page);
    }
    @RequestMapping("/insert")
    public BaseReturn insert(@RequestBody Rule rule){
        User user = getByToken();
        if(user == null){
            return BaseReturn.build("4003","登录失效");
        }
        rule.setCustomerOn(user.getCustomerNo());
        rule.setOperatorId(user.getId().intValue());
        boolean save = ruleService.save(rule);
        if(save){
            return BaseReturn.build("2000","添加成功");
        }else {
            return BaseReturn.build("5000","添加失败");
        }
    }

}

